<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/3/16 0016
 * Time: 10:03
 */

namespace English\Controller;

use Admin\Model\UserModel;
use Think\Controller;

class BaseController extends Controller
{
    protected $user_info;
    protected $user;
    public function  __construct()
    {
        parent::__construct();
        $this->user = new UserModel();
        if(!getSysStatus()){
            header("Content-type:text/html;charset=utf-8");
            exit('System maintenance....');
        }

        $this->user_info =  session('User_yctr');
        $info=session('User_yctr');
        if($info['status']==2){
            session('User_yctr',null);
            $this->redirect('Public/login');
        }
        if(!$this->user_info){

            $this->redirect('Public/login');
        }

    }

    /**
     * 代理等级
     * @param $configData
     * @param $userData
     * User: ming
     * Date: 2020/4/13 9:35
     */
    public function reward($configData,$userData,$zong_zong=0){
            $higher_user= M('user')->where(['id'=>$userData['higher_id']])->find();
            if($higher_user['reseller_level']==0){
                $rest_money=$higher_user['zuo_zong']-$zong_zong;
                if($rest_money>=$configData['a23'] && $higher_user['tj_count']>=$configData['a24']){//余下的是否满足条件
                    M('user')->where(['id'=>$userData['higher_id']])->save(['reseller_level'=>1]);
                }
            }elseif($higher_user['reseller_level']==1 && $higher_user['tj_count']>=$configData['a24']){//1级
                $count= M('user')->where(['higher_id'=>$userData['higher_id'],'reseller_level'=>1])->count();
                if($count>3){
                    M('user')->where(['id'=>$userData['higher_id']])->save(['reseller_level'=>2]);
                }
            }elseif($higher_user['reseller_level']==2 && $higher_user['tj_count']>=$configData['a24']){//2级
                $count= M('user')->where(['higher_id'=>$userData['higher_id'],'reseller_level'=>2])->count();
                if($count>3){
                    M('user')->where(['id'=>$userData['higher_id']])->save(['reseller_level'=>3]);
                }
            }elseif($higher_user['reseller_level']==3 && $higher_user['tj_count']>=$configData['a24']){//3级
                $count= M('user')->where(['higher_id'=>$userData['higher_id'],'reseller_level'=>3])->count();
                if($count>3){
                    M('user')->where(['id'=>$userData['higher_id']])->save(['reseller_level'=>4]);
                }
            }
    }
    /**
     * 人民币美元兑换
     * User: ming
     * Date: 2019/7/4 11:32
     */
    protected function finance_rate()
    {
        if (!S('rate')) {
            $url = "http://api.k780.com:88/?app=finance.rate&scur=USD&tcur=CNY&appkey=10003&sign=b59bc3ef6191eb9f747dd4e83c99f2a4";
            $Data = $this->https_request1($url);
            $rate = json_decode($Data, true);
            if ($rate['success'] == 1) {
                S('rate', $rate['result']['rate'], 43200);//12小时过期
                return $rate['result']['rate'];
            } else {
                return 6.8;
            }
        } else {
            return S('rate');
        }
    }


    /**
     * 实时价格API
     * @param string $type
     * @return mixed
     */
    protected function price_api($url)
    {

        //    $url = "http://www.coinyee.pro/app/v1/ThirdpartyController/markets?coin=".$type."&unit=cny&tdsourcetag=s_pcqq_aiomsg";
        // $url = " https://api.shenjian.io/?appid=ef09ed7301060858d32af37380bc8095&coin=bitcoin&currency=BTC";
//        $url = "http://api.coindog.com/api/v1/tick/Binance:".$type."USDT?unit=cny";
//       $url = "https://api.jinse.com/v3/coin/detail?slugs=tether&_source=m";
        $btcData = $this->https_request1($url);
        $btcData = json_decode($btcData, true);
//        $btcData['wfx']=$this->https_request1("https://api.mytokenapi.com/ticker/search?category=currency&keyword=Webflix&timestamp=1558063765845&code=15e926eac6ff75e8664ad3ca93a94a7a&platform=web_pc&v=1.0.0&language=zh_CN&legal_currency=CNY");
//        $btcData['USDT']=$this->https_request1("https://api.jinse.com/v3/coin/detail?slugs=tether&_source=m");

        return $btcData;
    }
    function https_request1($url,$data = null){
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        if (!empty($data)){
            curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        }
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($curl);
        curl_close($curl);
        return $output;
    }

    /**
     * 添加记录
     * @param $openid
     * @param $owner
     * @param $tot_money
     * @param $type
     * @param $reason
     * @param $reward_type
     * @param int $sxf
     * @param int $list_id
     * @return mixed
     * User: ming
     * Date: 2019/7/17 19:13
     */
    public function add_fmoney_log($openid,$owner,$tot_money,$type,$reason,$reward_type,$sxf=0,$list_id=0,$time=0){
        $fmoney['from_id'] = $openid;
        $fmoney['user_id'] = $owner;
        $fmoney['amount'] = $tot_money;
        $fmoney['create_time'] = time();
        $fmoney['type'] = $type;
        $fmoney['reward_type'] = $reward_type;
        $fmoney['tips'] = $reason;
        $fmoney['sxf'] = $sxf;//手续费
        $fmoney['list_id'] = $list_id;//手续费
        $fmoney['vip_time'] = $time;//手续费
        $data = M('fmoney')->add($fmoney);
        return $data;
    }

    protected function add_log($user_id,$amount=0,$type,$tips,$flag='+')
    {
        $map['user_id'] = $user_id;
        $map['amount'] = $amount;
        $map['type'] = $type;
        $map['create_time'] = time();

        //$map['account'] = $account;
        $map['flag'] = $flag;

        $dd = M('user')->find($map['user_id']);
        $type = 'z'.$type;
        $map['tips'] = $tips;
        return M('money_detail')->add($map);
    }

    protected function  add_fmoney($user_id,$from_id,$amount,$type,$reward_type,$tips)
    {
        $map['user_id'] =$user_id;
        $map['from_id'] = $from_id;
        $map['amount'] = $amount;
        $map['type'] = $type;
        $map['reward_type'] = $reward_type;
        $map['tips'] = $tips;
        $map['create_time'] = time();
        return M('fmoney')->add($map);
    }
    /**
     * 挂卖
     * @param $user_id
     * @param $sell_num |挂卖数量
     * @param $low_num |最低买入数量
     * @param int $lave |剩余数量
     * @param $sell |已卖出的数量
     * @param int $status
     * @param int $is_show
     * @return mixed
     */
    protected  function add_coin_sell($user_id,$sell_num,$low_num=0,$lave=0,$sell=0,$status=0,$is_show=1){
        $map['user_id'] =$user_id;
        $map['sell_num'] = $sell_num;
        $map['low_num'] = $low_num;
        $map['sell'] = $sell;
        $map['lave'] = $lave;
        $map['status'] = $status;
        $map['is_show'] = $is_show;
        $map['add_time'] = time();
        return M('coin_sell')->add($map);
    }

    /**
     * @param $user_id
     * @param $from_id
     * @param $sell_id
     * @param int $buy_num
     * @param int $status
     * @return mixed
     */
    protected  function add_coin_buy($user_id,$from_id,$sell_id,$buy_num=0,$status=0){
        $map['user_id'] =$user_id;
        $map['from_id'] = $from_id;
        $map['sell_id'] = $sell_id;
        $map['buy_num'] = $buy_num;
        $map['status'] = $status;
        $map['buy_time'] = time();
        return M('coin_buy')->add($map);
    }
    /**
     * 自动选择钱包地址20190627
     * @return array
     */
    public function auto_coin()
    {
        $id = rand(0,6);
        $user = [];
        switch ($id) {
            case 0:
                $user['from'] = '0xaC9D5FE3ce88862a405B8E268F36274Fb51d2C40';//平台钱包地址
                $user['key'] = '0x453eb3bd70ec907f06e0ebc5d815cdafd820d432e2c4bb726f6a3e0f3b9338a6';//平台钱包地址
                break;
            case 1:
                $user['from'] = '0x09a109BFD60dFcff815a7eDCe437AA8A046f93b7';//平台钱包地址
                $user['key'] = '0x5471650da26666e9b6767fae06fb6eaaf3a910de1b60d63d4f490dfeaa59fd95';//平台钱包地址
                break;
            case 2:
                $user['from'] = '0x82376574AA7DC5aD1944000ca3dbB434C065C76C';//平台钱包地址
                $user['key'] = '0xfd2b80f6099c2af52d840ab184d349566a7d839ad742919c549e7ce25e9f7235';//平台钱包地址
                break;
            case 3:
                $user['from'] = '0x80F27f9dEeead397C37eBaCd8093345cACd3D4Ae';//平台钱包地址
                $user['key'] = '0x312ca1ee395031c0715f2b5d7c328b1c86a23835d7ed7d6f665190aee9adc385';//平台钱包地址
                break;
            case 4:
                $user['from'] = '0xb7173c9683a4e142212B4A66C6F20b1Cb8baFbde';//平台钱包地址
                $user['from'] = '0x526b186f1d0eef404f704d32220a8955fb9d3078ec777e6ccf6b8a73150d2478';//平台钱包地址
                break;
            case 5:
                $user['from'] = '0x2795282E547D5b1e4d3E9fd653e45a8cC6327Ff8';//平台钱包地址
                $user['key'] = '0x20d47be257beb0051a21dd0d4ebd6fb551ffd41e47bf9bf5c8e33584c611b0e8';//平台钱包地址
                break;
            case 6:
                $user['from'] = '0x02Bf9DdAe4f10bC08E59DEF26344b11507E9E1A7';//平台钱包地址
                $user['key'] = '0x74be321efd09b1a689ec2647ddbce094815bcaf915f4ef5b8315f41445c2ac9f';//平台钱包地址
                break;
        }
        return $user;
    }
}
